package org.gnomus.data;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.datastore.Transaction;

public class item extends object {

    public item(Entity e) {
        super(e);
        setType((String) e.getProperty("type"));
        setTitle((String) e.getProperty("title"));
        if (e.getProperty("text") instanceof Text) {
            setText(((Text) e.getProperty("text")).getValue());
        } else if (e.getProperty("text") instanceof String){
            setText((String) e.getProperty("text"));
        }
        setOrder((String) e.getProperty("order"));
        if ("info".equals(getType())) {
            setGraphics((String) e.getProperty("graphics"));
        } else if ("test".equals(getType())) {
            setTask((String) e.getProperty("task"));
        }
    }
    
    public item(String title, String type, String order) {
        super("item");
        setType(type);
        setTitle(title);
        setText("No text yet.");
        setOrder(order);
    }

/*
    public item(JSONObject jo) {
        super("item");
        try {
            setType(jo.getString("type"));
            setTitle(jo.getString("title"));
            setText(jo.getString("text"));
            setOrder(jo.getString("order"));
            if ("info".equals(getType())) {
                setGraphics(jo.getString("graphics"));
            } else if ("test".equals(getType())) {
                setTask(jo.getString("task"));
            }
        } catch (JSONException e) {
            throw new Error(e);
        }
    }
*/

    public void delete() {
        Transaction txn = store.beginTransaction();
        try {
            link.delete(key);
            store.delete(key);
            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
    }

    public void setType(String type) {
        put("type", type);
    }

    public String getType() {
        return (String) get("type");
    }

    public void setTitle(String title) {
        put("title", title);
    }

    public String getTitle() {
        return (String) get("title");
    }

    public void setText(String text) {
        put("text", text);
    }

    public String getText() {
        return (String) get("text");
    }

    public void setOrder(String order) {
        put("order", order);
    }

    public String getOrder() {
        return (String) get("order");
    }

    public void setGraphics(String graphics) {
        put("graphics", graphics);
    }

    public String getGraphics() {
        return (String) get("graphics");
    }
    public void setTask(String task) {
        put("task", task);
    }

    public String getTask() {
        return (String) get("task");
    }
}
